﻿
using System ;
using System.Drawing ;

using MskEdit.Msk.Representation ;


namespace MskEdit.Msk.Model
{
    public class MskLine
    {
    
        #region Private

        private         MskLineKey  lineKey_ ;
        private         string      line_ ;
        private static  MskLine     nullLine_ = new MskLine() ;
    
        
        private static string getNullLine() {
        
            string line = "" ;
            line = StringField.FieldName.AppendTo( line, "" ) ;
            line = StringField.Len.AppendTo( line, "0" ) ;
            line = StringField.Yk.AppendTo( line, "0" ) ;
            line = StringField.Xk.AppendTo( line, "0" ) ;
            line = StringField.Type.AppendTo( line, "" ) ;
            line = StringField.Decimals.AppendTo( line, "0" ) ;
            line = StringField.PageCode.AppendTo( line, "0" ) ;
            line = StringField.Caption.AppendTo( line, "" ) ;
            line = StringField.FunCode.AppendTo( line, "0" ) ;
            line = StringField.AcadY.AppendTo( line, "0" ) ;
            line = StringField.AcadX.AppendTo( line, "0" ) ;
            line = StringField.AcadHeight.AppendTo( line, "0" ) ;
            line = StringField.AcadStyle.AppendTo( line, "0" ) ;
            line = StringField.AcadAngle.AppendTo( line, "0" ) ;
            line = StringField.AcadLayer.AppendTo( line, "0" ) ;
            line = StringField.AcadAlignment.AppendTo( line, "0" ) ;
            line = StringField.HelpText.AppendTo( line, "0" ) ;
            line = StringField.PresetValue.AppendTo( line, "0" ) ;
            line = StringField.AcadY2.AppendTo( line, "0" ) ;
            line = StringField.AcadX2.AppendTo( line, "0" ) ;
            line = StringField.AcadBlock.AppendTo( line, "0" ) ;
            line = StringField.AcadTag.AppendTo( line, "0" ) ;
            line = StringField.Language.AppendTo( line, "0" ) ;

            return line ;
        }
        
        private MskLine () {
        
            lineKey_    = MskLineKey.NullLineKey ;
            line_       = getNullLine() ;
        }
        
        private MskLine( MskLineKey lineKey, string line ) {
            lineKey_    = lineKey ;
            line_       = line ;
        }
        
        #endregion

        #region Public

        public static MskLine TryFrom( string line, MskLine fallbackValue ) {
        
            MskLineKey lineKey = MskLineKey.TryFrom( line, null ) ;
            if ( lineKey == null )
                return fallbackValue ;
                
            return new MskLine( lineKey, line ) ;
        }
        
        override public string ToString() {
            return line_ ;
        }

        #endregion

        #region Public properties
        
        public static MskLine NullLine {
            get { return nullLine_ ; }
        }
    
    
        public MskLineKey LineKey {
            get { return MskLineKey.TryFrom( line_, MskLineKey.NullLineKey ) ; }
        }
    
    
        public string FieldName {
            get { return StringField.FieldName.TryFrom( line_, "" ) ; }
        }
    
    
        public int Len {
            get { return IntField.Len.TryFrom( line_, 0 ).Value ; }
        }
    
    
        public Point XYk {
            get { return PointField.XYk.TryFrom( line_, new Point( 0,0 ) ).Value ; }
            set { line_ = PointField.XYk.TryInsert( line_, value, line_ ) ; }
        }
    
    
        public string Type {
            get { return StringField.Type.TryFrom( line_, "" ) ; }
        }
    
    
        public string Caption {
            get { return StringField.Caption.TryFrom( line_, "" ).Trim() ; }
            set { line_ = StringField.Caption.TryInsert( line_, value, line_ ) ; }
        }
    
    
        public int Decimals {
            get { return IntField.Decimals.TryFrom( line_, 0 ).Value ; }
        }
    
    
        public MskPageCode PageCode {
            get { return MskPageCode.TryFrom( line_, MskPageCode.NullPageCode ) ; }
        }
    
    
        public MskFunctionCode FunctionCode {
            get { return MskFunctionCode.TryFrom( line_, MskFunctionCode.NullFunctionCode ) ; }
        }
    
    
        public PointF AcadXY {
            get { return PointFField.AcadXY.TryFrom( line_, new PointF( 0,0 ) ).Value ; }
        }
    
    
        public string AcadHeight {
            get { return StringField.AcadHeight.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadStyle {
            get { return StringField.AcadStyle.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadAngle {
            get { return StringField.AcadAngle.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadLayer {
            get { return StringField.AcadLayer.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadAlignment {
            get { return StringField.AcadAlignment.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string HelpText {
            get { return StringField.HelpText.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadBlock {
            get { return StringField.AcadBlock.TryFrom( line_, "" ).Trim() ; }
        }
    
    
        public string AcadTag {
            get { return StringField.AcadTag.TryFrom( line_, "" ).Trim() ; }
        }

        #endregion
    }
}
